local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local json  = require("libs.json")
local theme = require("theme")
local dpi	= require("beautiful.xresources").apply_dpi




local weather = {}
local update_timeout = 300
local cmd_get_weather = "curl --connect-timeout 5 \"https://restapi.amap.com/v3/weather/weatherInfo?key=`cat ~/.config/Amap/key`&city=330382\" || echo failed"




weather.widget = wibox.widget{
	{
		{
			{
				{
					id   = "weather",
					text = " -- ",
					font = theme.weather_font,
					widget = wibox.widget.textbox,
				},
				{
					left = dpi(10),
					widget	= wibox.container.margin,
				},
				{
					id   = "temperature",
					text = " -- ",
					font = theme.weather_font,
					widget = wibox.widget.textbox,
				},
				{
					left = dpi(10),
					widget	= wibox.container.margin,
				},
				{
					id   = "humidity",
					text = " -- ",
					font = theme.weather_font,
					widget = wibox.widget.textbox,
				},
				layout = wibox.layout.fixed.horizontal,
			},
			left   = dpi(15),
			right  = dpi(15),
			widget	= wibox.container.margin,
		},
		id = "background",
		fg = theme.widget_fg,
		bg = theme.widget_bg,
		shape = function(cr, width, height)
			gears.shape.rounded_rect(cr, width, height, theme.widget_radius)
		end,
		widget = wibox.container.background,
	},
	margins = theme.widget_margins,
	widget	= wibox.container.margin,
	set_bg = function(self, var)
		self:get_children_by_id("background")[1].bg = var
	end,
	set_fg = function(self, var)
		self:get_children_by_id("background")[1].fg = var
	end
}


function weather:update()
	awful.spawn.easy_async_with_shell(cmd_get_weather, function(out)
		--print(os.date("%X") .. ": update weather")
		--print(out)
		if string.match(out,"failed") then
			print("weather: curl failed")
			self.reconnect_timer:start()
			return
		end
		local data = json.decode(out)
		if data.status == "0" then
			print("weather: status failed")
			return
		end
		if data.lives[1].weather ~= nil then
			self.widget:get_children_by_id("weather")[1].text = data.lives[1].weather .. " "
			--self.widget:get_children_by_id("weather")[1].text = string.format("%s • %s℃  • %s%%", data.lives[1].weather, data.lives[1].temperature, data.lives[1].humidity)
		end
		if data.lives[1].temperature ~= nil then
			self.widget:get_children_by_id("temperature")[1].text = data.lives[1].temperature .. "℃ "
		end
		if data.lives[1].humidity ~= nil then
			self.widget:get_children_by_id("humidity")[1].text = data.lives[1].humidity .. "%"
		end
	end)
end



function weather:setup()
	self.widget:connect_signal('mouse::enter',function() 
		self.widget.bg = theme.widget_bg_hover
	end)
	self.widget:connect_signal('mouse::leave',function() 
		self.widget.bg = theme.widget_bg
	end)

	self.timer = gears.timer({
		timeout   = update_timeout,
		call_now  = false,
		autostart = true,
		callback  = function()
			--print("weather: update")
			self:update()
		end
	})

	self.reconnect_timer = gears.timer({
		timeout   = 5,
		call_now  = false,
		autostart = true,
		single_shot = true,
		callback  = function()
			--print("weather: reconnect")
			self:update()
		end
	})

	return self.widget
end




return weather
